﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.IO;
using System.ComponentModel;

namespace PacketBuilder
{
    /// <summary>
    /// Interaction logic for HeaderOptions.xaml
    /// </summary>
    public partial class HeaderOptions : UserControl, INotifyPropertyChanged
    {
        private List<string> interfaces;

        public List<string> Interfaces
        {
            get { return interfaces; }
            set {
                interfaces = value;
                OnPropertyChanged("interfaces"); 
            }
        }

        public HeaderOptions()
        {
            InitializeComponent();
            Interfaces = GetDeviceList();
        }

        private List<string> GetDeviceList()
        {
            var args = new StringBuilder();
            args.AppendFormat(" --{0}", MainWindow.stringOptGetDeviceList);
            MainWindow.RunCmd(MainWindow.stringCommandToolName, args.ToString());
            if (File.Exists(MainWindow.stringDeviceListFile))
            {
                return File.ReadAllLines(MainWindow.stringDeviceListFile).ToList<string>();
            }
            else
            {
                return new List<string> { MainWindow.stringErrorDevicesHaveNotBeenFound };
            }
        }

        private void ProtocolSelected(object sender, SelectionChangedEventArgs e)
        {
            var senderComboBox = sender as ComboBox;
            var selectedItem = senderComboBox.SelectedItem as TextBlock;
            var protocolName = selectedItem.Tag as string;
            LoadOptionsMap(protocolName);
            MainWindow.optionsStackPanel.Children.Clear();
            MainWindow.optionsStackPanel.Children.Add(GetProtocolOptions(protocolName));            
        }

        private UIElement GetProtocolOptions(string protocolName)
        {            
            switch (protocolName)
            {
                case "icmp":
                    return new PacketInfoICMP();                    
                case "ipv4":
                    return new PacketInfoIPv4();                    
                case "igmpv2":
                    return new PacketInfoIGMPv2();                   
                case "arp":
                    return new PacketInfoARP();
                case "ethernet":
                    return new PacketInfoEthernet();                    
                case "rip":
                    return new PacketInfoRIP();                    
                case "udp":
                    return new PacketInfoUDP();                    
                case "tcp":
                    return new PacketInfoTCP();                    
                default:
                    throw new Exception(MainWindow.stringExceptionUnknownProtocol);                    
            }
        }

        public static void LoadOptionsMap(string selectedProtocol)
        {        

            if (string.Empty != MainWindow.lastSelectedProtocol)
            {
                MainWindow.optionsMapsMap[MainWindow.lastSelectedProtocol] = MainWindow.optionsMap;
            }
            MainWindow.lastSelectedProtocol = selectedProtocol;
            if (true == MainWindow.optionsMapsMap.ContainsKey(MainWindow.lastSelectedProtocol))
            {
                MainWindow.optionsMap = MainWindow.optionsMapsMap[MainWindow.lastSelectedProtocol];
            }
            else
            {
                MainWindow.optionsMap = new Dictionary<string, string>();
            }
        }

        private void PacketsNumberSelected(object sender, TextChangedEventArgs e)
        {
            MainWindow.TextBoxChanged(sender, e);
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion

        private void DeviceSelected(object sender, SelectionChangedEventArgs e)
        {
            MainWindow.ComboBoxStateChanged(sender, e);
        }
    }
}
